//
//  LoadingView.swift
//  AIImage
//
//  Created by deng on 2023/12/10.
//

import UIKit

class LoadingView: UIView {
    private let loadingIndicator: UIActivityIndicatorView
    private let label: UILabel
    
    init(frame: CGRect, text: String) {
        loadingIndicator = UIActivityIndicatorView(style: .large)
        label = UILabel()
        
        super.init(frame: frame)
        
        backgroundColor = UIColor.black.withAlphaComponent(1.0)
        layer.cornerRadius = 10
        layer.masksToBounds = true
        
        loadingIndicator.center = CGPoint(x: bounds.midX, y: bounds.midY-14*DS.s)
        addSubview(loadingIndicator)
        
        label.frame = CGRect(x: 0, y: loadingIndicator.frame.maxY + 5*DS.s, width: bounds.width, height: 20*DS.s)
        label.textAlignment = .center
        label.textColor = UIColor.white
        label.text = text
        addSubview(label)
        
        startLoading()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func startLoading() {
        loadingIndicator.startAnimating()
    }
    
    func stopLoading() {
        loadingIndicator.stopAnimating()
        removeFromSuperview()
    }
}
